package com.tensquare.article.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wz
 * @company 公司
 * @create 2020-02-07 16:07
 */
@RestController
@RequestMapping("article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisTemplate redisTemplate;

    //根据文章id和用户id,建立订阅关系.保存 文章作者id和用户id的关系
    @PostMapping("subscribe")
    public Result subscribe(@RequestBody Map map) {
        //返回订阅状态 true  false
        Boolean flag = articleService.subscribe(map.get("articleId").toString(), map.get("userId").toString());
        //判读是否订阅
        if (flag) {
            return new Result(true,StatusCode.OK,"订阅成功");
        } else {
            return new Result(false,StatusCode.ERROR,"取消订阅成功");
        }
    }

    //测试公共异常处理
    @GetMapping("exception")
    public Result test() {
        int a = 1 / 0;
        return null;
    }

    //GET article 文章全部列表
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Article> list = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    //GET article/{articleId} 根据ID查询文章
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        Article article = articleService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", article);
    }

    //新增文章
    @PostMapping
    public Result save(@RequestBody Article article) {
        articleService.save(article);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    //修改文章
    @PutMapping("/{id}")
    public Result update(@PathVariable String id, @RequestBody Article article) {
        article.setId(id);
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //删除文章
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        articleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //条件查询和分页
    //采用集合方式接收pojo是因为,我们需要对条件进行非空判断,便利pojo需要用到反射成本较高,所以采用集合方式进行接收
    @PostMapping("search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page, @PathVariable Integer size,
                             @RequestBody Map<String, Object> map) {
        //根据条件分页查询
        Page<Article> pageData = articleService.findBYPage(map, page, size);
        //封装分页返回对象
        PageResult<Article> pageResult = new PageResult<>(pageData.getTotal(), pageData.getRecords());
        //返回数据
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    //文章点赞
    @PutMapping("thumbup/{articleId}")
    public Result thumbup(@PathVariable("articleId")String articleId){
        //模拟用户id
        String userId = "4";
        String key = "thumbup_article_"+userId+"_"+articleId;

        //查询用户点赞信息
        Object flag = redisTemplate.opsForValue().get(key);

        if (flag==null){
            //没有点过赞可以点赞
            articleService.thumbup(articleId);
            //点赞成功保存点赞信息
            redisTemplate.opsForValue().set(key,1);

            return new Result(true,StatusCode.OK,"点赞成功");
        }else {
            return new Result(false,StatusCode.REPERROR,"不能重复点赞");
        }
    }
}
